/*
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file manager.h
 *
 * @author Marco Buzzanca
 * @author Davide Giuseppe Monaco
 * 
 * This header file contains Manager class declaration.
 */

#ifndef _MANAGER_H_
#define _MANAGER_H_ 1

#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream>
#include <vector>

#include "address.h"
#include "activity.h"
#include "discoveryproxyfactory.h"
#include "udpserver.h"
#include "wfqueuemanager.h"
#include "workernetwork.h"
#include "workflow.h"

/**
 * @brief Manager has an important role in the system.
 * UI submits Workflows to be executed. The Manager receives these submissions
 * and creates Activities dispatching them to Workers, properly chosen through
 * Discovery.
 */
class Manager
{
private:
  /**
   * @brief Used to get WorkerDiscoveryProxy and its getWorkers() service.
   */
  DiscoveryProxyFactory _dpf;

  /**
   * @brief Discovery address.
   */
  Address _discovery_addr;

  /**
   * @brief WFQueueManager object used to handle remote enqueue() requests.
   */
  WFQueueManager _wfqm;

  /**
   * @brief Worker thread group.
   */
  boost::thread_group _workers;

  /**
   * @brief worker-thread callback member function.
   * @param id  Mainly used for debug purpose.
   *
   * When some Workflow is dequeued from local WFQueueManager get the proxy
   * to Discovery, create a WorkerNetwork and associate it to a new Activity
   * along with the dequeued Workflow.
   * Finally call scheduleActivity() on the brand new Activity.
   */
  void serve(int id);

public:
  /**
   * @brief Constructor. Default port value for WFQueueManager is 12000.
   */
  Manager(boost::uint16_t port = 12000);

  /**
   * @brief Constructor specifying Address object.
   * @param address The provided Address object.
   */
  Manager(const Address& address);

  /**
   * @brief Destructor.
   */
  ~Manager(void);

  /**
   * @brief Gets server port.
   */
  boost::uint16_t getPort(void) const;

  /**
   * @brief Sets server port to the provided one.
   *
   * @param port The provided port.
   */
  void setPort(boost::uint16_t port);

  /**
   * @brief Sets server port using provided Address.
   *
   * @param address The provided Address.
   */
  void setPort(const Address& address);

  /**
   * @brief Set Address, providing IP address and port, to use on DiscoveryProxy.
   *
   * @param IP The string containing Discovery IP address.
   * @param port The provided port.
   */
  void setDiscoveryAddress(std::string IP, boost::uint16_t port);

  /**
   * @brief Starts the queue manager and the forwarding service.
   */
  void run(void);
};

#endif
